package com.park.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.park.model.CouponRecordData;
import com.park.model.Paging;
import com.park.service.CouponRecordDataService;
import com.park.service.Utility;
import com.park.service.impl.CouponRecordDataServiceImpl;

@Controller
@RequestMapping("couponRecordData")
public class CouponRecordDataController {

	@Autowired
	private CouponRecordDataService couponRecordDataService;
	private static Log logger = LogFactory.getLog(CouponRecordDataController.class);
	
	@RequestMapping(value = "/insert", method = RequestMethod.POST, produces = { "application/json;charset=utf-8" })
	@ResponseBody
	public String insert(@RequestBody CouponRecordData couponRecordData) {
		logger.info("[insert]参数:" + couponRecordData);
		Map<String, Object> resultMap = new HashMap<>();
		int num = couponRecordDataService.insert(couponRecordData);
		if (num == 1) {
			resultMap.put("status", 1001);
			resultMap.put("message", "添加成功");
		} else {
			resultMap.put("status", 1002);
			resultMap.put("message", "添加失败");
		}
		return Utility.gson.toJson(resultMap);
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST, produces = { "application/json;charset=utf-8" })
	@ResponseBody
	public String update(@RequestBody CouponRecordData couponRecordData) {
		logger.info("[update]参数:" + couponRecordData);
		Map<String, Object> resultMap = new HashMap<>();
		int num = couponRecordDataService.update(couponRecordData);
		if (num == 1) {
			resultMap.put("status", 1001);
			resultMap.put("message", "修改成功");
		} else {
			resultMap.put("status", 1002);
			resultMap.put("message", "修改失败");
		}
		return Utility.gson.toJson(resultMap);
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST, produces = { "application/json;charset=utf-8" })
	@ResponseBody
	public String delete(@RequestBody Map<String, Object> args) {
		logger.info("[delete]参数:" + args);
		Map<String, Object> resultMap = new HashMap<>();
		List<Integer> id = (List<Integer>) args.get("id");
		int num = couponRecordDataService.delete(id);
		if (num == id.size()) {
			resultMap.put("status", 1001);
			resultMap.put("message", "删除成功");
		} else {
			resultMap.put("status", 1002);
			resultMap.put("message", "删除失败");
		}
		return Utility.gson.toJson(resultMap);
	}

	@RequestMapping(value = "/select", method = RequestMethod.POST, produces = { "application/json;charset=utf-8" })
	@ResponseBody
	public String select(@RequestBody Map<String, Object> args) {
		logger.info("[delete]参数:" + args);
		Map<String, Object> resultMap = new HashMap<>();
		int startPage = (int) args.get("startPage");
		int pageSize = (int) args.get("pageSize");
		List<CouponRecordData> videoRecordings = couponRecordDataService.select((startPage - 1) * pageSize, pageSize);
		int allCount = couponRecordDataService.getAllCount();
		Paging page = new Paging(startPage, pageSize, allCount, videoRecordings);
		resultMap.put("status", 1001);
		resultMap.put("data", page);
		resultMap.put("message", "查询成功");
		return Utility.gson.toJson(resultMap);
	}
}
